<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Grid Rendering</title>
  <style>
    canvas {
      border: 1px solid #ccc;
    }
  </style>
</head>

<body>
  <canvas id="gridCanvas" width="500" height="500"></canvas>

  <script>
    // 示例二维数组，包含x和y坐标
    const gridData = [
      [{ x: 50, y: 50 }, { x: 150, y: 50 }, { x: 250, y: 50 }],
      [{ x: 50, y: 150 }, { x: 150, y: 150 }, { x: 250, y: 150 }],
      [{ x: 50, y: 250 }, { x: 150, y: 250 }, { x: 250, y: 250 }]
    ];

    const canvas = document.getElementById("gridCanvas");
    const ctx = canvas.getContext("2d");

    function drawGrid (data) {
      // 遍历二维数组
      for (let i = 0; i < data.length; i++) {
        for (let j = 0; j < data[i].length; j++) {
          const point = data[i][j];

          // 绘制点
          ctx.beginPath();
          ctx.arc(point.x, point.y, 5, 0, Math.PI * 2);
          ctx.fillStyle = "blue";
          ctx.fill();

          // 连接右侧相邻点
          if (j < data[i].length - 1) {
            const rightNeighbor = data[i][j + 1];
            ctx.beginPath();
            ctx.moveTo(point.x, point.y);
            ctx.lineTo(rightNeighbor.x, rightNeighbor.y);
            ctx.strokeStyle = "black";
            ctx.stroke();
          }

          // 连接下方相邻点
          if (i < data.length - 1) {
            const belowNeighbor = data[i + 1][j];
            ctx.beginPath();
            ctx.moveTo(point.x, point.y);
            ctx.lineTo(belowNeighbor.x, belowNeighbor.y);
            ctx.strokeStyle = "black";
            ctx.stroke();
          }
        }
      }
    }

    // 绘制网格
    drawGrid(gridData);
  </script>
</body>

</html>